package net.chathu.airgw.packets.gw.clientpackets 
{
	import net.chathu.airgw.model.DisturbState;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.EchoPacket;
	import net.chathu.airgw.packets.gw.serverpackets.EndOfWhoisPacket;
	import net.chathu.airgw.packets.gw.serverpackets.ErrorNoSuchNickOrChannelPacket;
	import net.chathu.airgw.packets.gw.serverpackets.WhoisChannelsPacket;
	import net.chathu.airgw.packets.gw.serverpackets.WhoisIdlePacket;
	import net.chathu.airgw.packets.gw.serverpackets.WhoisServerPacket;
	import net.chathu.airgw.packets.gw.serverpackets.WhoisUserPacket;
	/**
	 * ...
	 * @author ze
	 */
	public class CMDWhois extends IRCReceivablePacket
	{
		
		public function CMDWhois() 
		{
			
		}
		
		override public function execute():void {
			if (data.length > 1)
			{
				var names:Array = String(data[1]).split(",");
				
				for (var i:int = 0; i < names.length; i++)
				{
					var name:String = names[i];
					var user:User = User(sessionData.chatData.users.getUserByNameIgnoreCase(name));
					if (user)
					{
						sendToClient(new WhoisUserPacket(sessionData.nick, user.userHost));
						var joinedRooms:Array = sessionData.chatData.getCommonJoinedRoomsWithUser(user.id);
						if (joinedRooms.length > 0)
						{
							var whoisChannelsReply:String = "";
							for each(var joinedRoom:JoinedRoomInfo in joinedRooms)
							{
								//TODO: Jogokat hozzafuzni! 
								//:atw.irc.hu 319 test343 test343 :@#dfsdfsdf 
								whoisChannelsReply += sessionData.chatData.rooms.getRoom(joinedRoom.roomId).name + " ";
							}
							sendToClient(new WhoisChannelsPacket(sessionData.nick, user.name, whoisChannelsReply));
						}
						sendToClient(new WhoisServerPacket(sessionData.nick, user.name));
						var onlineSince:int = (new Date().getTime() - user.connectedTime.getTime()) / 1000; //online since (in seconds)
						sendToClient(new WhoisIdlePacket(sessionData.nick, user.name, onlineSince));
						sendToClient(new EchoPacket(sessionData.nick,user.name + " RealNick: " + user.originalName +" (CurrentId:" + user.id + ") Nem: " + user.getGender() + " Kor: " + (user.age == 0 ? "Titkos" : user.age) + " Szín: " + user.color + " Állapot: " + DisturbState.getStateByCode(user.disturb).description  ));
						sendToClient(new EchoPacket(sessionData.nick, user.name + " adatlapja: http://chat.hu/user/" + user.regId));
					} else {
						sendToClient(new ErrorNoSuchNickOrChannelPacket(sessionData.nick, name));
					}
				}
				sendToClient(new EndOfWhoisPacket(sessionData.nick, data[1]));
			}
		}
		
		public static function getShortUsageDescription():String {
			return "Gyors adatlap";
		}
		
		public static function getUsageDescription():String {
			return "Példa:\n" +
				"/userinfo gipsz_jakab\n\n" +
				"Eredmény:\n\n" + 
				"[gipsz_jakab!~gipsz_jakab@F18.1234567.chat.hu] (@):\n" + 
				"[gipsz_jakab] AirGW :with a direct connection to ARPANET military network.\n" + 
				"[gipsz_jakab] tétlen 01:02:03\n" + 
				"[gipsz_jakab] RealNick: gipsz_jakab (CurrentId:12345) Nem: Férfi Kor: 18 Szín: FFFBF4 Állapot: Ne zavarj!\n" + 
				"[gipsz_jakab] adatlapja: http://chat.hu/user/1234567\n" + 
				"[gipsz_jakab] WHOIS lista vége.";
		}
	}
}